package com.practice.config;

import com.practice.dao.daoImpl.EmpDaoImpl;
import com.practice.service.serviceImpl.EmpServiceImpl;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author:高杰领
 * @date:2021/11/17 15:32
 * @description:spring的配置类,替换之前的xml配置类
 * @version:1.0
 **/
@Configuration
@ComponentScan(basePackages = "com.practice")
@PropertySource(value = "classpath:db.properties")
public class SpringConfig {
    /**
     *
     * @param null
     * @return
     * @author 高冷
     * @description:加载配置文件中的数据库信息
     * @date 2021/11/17 16:42
     */
    @Value("${url}")
    private String url;
    @Value("${driverClassName}")
    private String driverClassName;
    @Value("${name}")
    private String name;
    @Value("${password}")
    private String password;
    /**
     *
     * @param
     * @return org.apache.commons.dbcp2.BasicDataSource
     * @author 高冷
     * @description:初始化数据源
     * @date 2021/11/17 16:43
     */
    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(this.url);
        basicDataSource.setDriverClassName(this.driverClassName);
        basicDataSource.setUsername(this.name);
        basicDataSource.setPassword(this.password);
        return  basicDataSource;
    }
    /**
     *
     * @param
     * @return org.springframework.jdbc.core.JdbcTemplate
     * @author 高冷
     * @description:配置jdbcTemplate
     * @date 2021/11/17 16:44
     */
    @Bean()
    public JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(this.dataSource());
        return jdbcTemplate;
    }
    /**
     *
     * @param
     * @return com.practice.dao.daoImpl.EmpDaoSpringJDBCImpl
     * @author 高冷
     * @description:创建empdao的bean
     * @date 2021/11/17 16:55
     */
    @Bean("empDao")
    public EmpDaoImpl empDao(){
        EmpDaoImpl empDaoSpringJDBC = new EmpDaoImpl();
        empDaoSpringJDBC.setJdbcTemplate(this.jdbcTemplate());
        return empDaoSpringJDBC;
    }
    @Bean("empService")
    public EmpServiceImpl empService(){
        EmpServiceImpl empService = new EmpServiceImpl();
        empService.setEmpDao(this.empDao());
        return  empService;
    }

//    public static void main(String[] args) {
//        SpringConfig springConfig = new SpringConfig();
//        System.out.println(springConfig.empService());
//        System.out.println(springConfig.empService().getEmpDao());
//    }
}
